package com.qf.web.controller;

import com.alibaba.fastjson.JSON;
import com.qf.domain.GoodsType;
import com.qf.service.GoodTypeService;
import com.qf.service.impl.GoodTypeServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/goodstypeservlet")
public class GoodsTypeServlet extends BaseServlet {
    //公开方法，反射才能够访问
    //ajax获取商品类别列表
    public String goodstypelist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");

        ServletContext application = this.getServletContext();//每次都从数据库找效率低，存一下，下一次请求直接给 域对象整个项目可见
        String goodsType = (String)application.getAttribute("goodsType");
        if (goodsType != null) {
            response.getWriter().write(goodsType);//不为空直接获取输出
            System.out.println("从application中获取数据");
            return null;//获取完就结束了
        }
        //创建业务对象
       GoodTypeService goodTypeService = new GoodTypeServiceImpl();
       //调用方法
       List<GoodsType> goodsTypes = goodTypeService.findByLevel(1);//返回级别1的类型
        System.out.println("从数据库中获取数据");
        //把集合转为json字符串（前端要json数据）
        String json = JSON.toJSONString(goodsTypes);

        //json回写给浏览器
        response.getWriter().write(json);
        //json数据存入域对象
        application.setAttribute("goodsType",json);
        return null;
    }
    public  String getGoodsType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        GoodTypeService goodTypeService=new GoodTypeServiceImpl();
        List<GoodsType> goodsTypes= goodTypeService.findAll();
        request.setAttribute("goodsTypeList",goodsTypes);
        return  "/admin/showGoodsType.jsp";
    }
    //点击添加按钮后的回显
    public  String addGoodsType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        GoodTypeService goodTypeService=new GoodTypeServiceImpl();
        List<GoodsType> goodsTypes= goodTypeService.findAll();
        request.setAttribute("goodsTypeList",goodsTypes);
        return  "/admin/addGoodsType.jsp";
    }
    public  String addtype(HttpServletRequest request, HttpServletResponse response) throws Exception{

        String goodsParent = request.getParameter("goodsParent");
        String typename = request.getParameter("typename");
        GoodTypeService goodTypeService=new GoodTypeServiceImpl();
        goodTypeService.insert(goodsParent,typename);
        List<GoodsType> goodsTypes = goodTypeService.findAll();
        ServletContext application = this.getServletContext();
        request.setAttribute("goodsTypeList",goodsTypes);
        return  "/admin/showGoodsType.jsp";
    }
    public String searchGoodsTypes(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        StringBuilder where1=new StringBuilder(" where 1=1 ");
        String level = request.getParameter("level");
        String name = request.getParameter("name");
        List<Object> I=new ArrayList();
        if (!StringUtils.isEmpty(name))
        {
            where1.append(" and name like ? ");
            I.add("%"+name+"%");
        }
        if (!StringUtils.isEmpty(level))
        {

            where1.append(" and level = ? ");
            I.add(Integer.parseInt(level));
        }
        GoodTypeService goodTypeService=new GoodTypeServiceImpl();
        List<GoodsType> goodsTypes= goodTypeService.selectByNAL(where1,I);
        request.setAttribute("goodsTypeList",goodsTypes);
        return  "/admin/showGoodsType.jsp";
    }
    public String updateGoodsType(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String level = request.getParameter("level");
        String parent = request.getParameter("parent");
        GoodsType goodsType=new GoodsType(Integer.parseInt(id),name,Integer.parseInt(level),Integer.parseInt(parent));
        GoodTypeService goodTypeService=new GoodTypeServiceImpl();
        goodTypeService.update(goodsType);
        List<GoodsType> goodsTypes= goodTypeService.findAll();
        request.setAttribute("goodsTypeList",goodsTypes);
        return "/admin/showGoodsType.jsp";
    }
}






